package com.pri.chat.activity;

import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.LoginBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.model.CodeModel;
import com.pri.chat.model.RegModel;
import com.pri.chat.utils.Des3Util;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 手机号
 */
public class RegisterByPhoneActivity extends BaseActivity {


    @BindView(R.id.et_login_name)
    EditText etLoginName;
    @BindView(R.id.iv_clean_phone)
    ImageView ivCleanPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.title)
    TextView tvTitle;


    private TimeCount time;
    private String mCodeId;
    private boolean isFinish;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register_by_phone;
    }

    @Override
    protected void initView() {
        time = new TimeCount(60000, 1000);
        tvTitle.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isFinish = true;
        time.cancel();
    }


    @OnClick({R.id.tv_code, R.id.btn_login})
    public void onViewClicked(View view) {
        String mobile = etLoginName.getText().toString();
        String code = etCode.getText().toString();
        switch (view.getId()) {
            case R.id.tv_code:
                if (mobile.isEmpty()) {
                    RxToast.normal("手机号为空");
                } else {
                    sendCode(mobile);
                }
                break;
            case R.id.btn_login:
                if (mobile.isEmpty()) {
                    RxToast.normal(getString(R.string.login_mobile_empty));
                } else if (code.isEmpty()) {
                    RxToast.normal(getString(R.string.login_pwd_empty));
                } else {
//                    login(mobile, code);
                    RxActivityTool.skipActivity(this, CompleteMaterialActivity.class);
                }
                break;
        }
    }

    /**
     * 发送验证码
     *
     * @param mobile 手机号
     */
    private void sendCode(String mobile) {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            Log.d(TAG, "onNext: " + baseBean.toString());
            mCodeId = (String) baseBean.getData();
            time.start();
            RxToast.normal("验证码发送成功");
        };
        CodeModel codeModel = new CodeModel(mobile, 0);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(codeModel));
        HttpMethods.getInstance().sendCode(new ProgressSubscriber<>(onNextListener, this, true), requestBody);
    }


    /**
     * 验证码登陆
     *
     * @param mobile
     * @param code
     */
    private void login(final String mobile, final String code) {
        SubscriberOnNextListener<BaseBean<LoginBean>> onNextListener = baseBean -> {
            dismissProgressDialog();
            if (baseBean.isSuccess()) {
                //TODO 完善资料
                RxActivityTool.skipActivity(this, CompleteMaterialActivity.class);
            } else {
                RxToast.normal(baseBean.getMsg());
            }
        };
        RegModel regModel = new RegModel();//登录上传参数
        regModel.setMobile(mobile);
        regModel.setSendId(mCodeId);
        regModel.setSendId(code);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(regModel));
        HttpMethods.getInstance().login(new ProgressSubscriber<>(onNextListener, this, true), requestBody);
    }

    class TimeCount extends CountDownTimer {

        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {

            tvCode.setClickable(false);
            tvCode.setText("(" + millisUntilFinished / 1000 + ") 秒后可发送");
        }

        @Override
        public void onFinish() {
            if (!isFinish) {
                tvCode.setText("重新获取");
                tvCode.setClickable(true);
            }


        }
    }
}